# ---------------------------------------------------------------------------
# Author: Keith Hill based on a Start-Debug script by Jeff Jones (MSFT).
# Desc:   Set of debug functions that allow you to set breakpoints in a
#         script as well as temporarily disable/enable and skip breakpoints.
# Date:   Nov 18, 2006
# Site:   http://www.codeplex.com/PowerShellCX
# Usage:  In your profile place the following command:
#  
#         . "$Env:PscxHome\Profile\Debug.ps1"
# ---------------------------------------------------------------------------
$global:__pscxDebugBreakpointsEnabled=$true
$global:__pscxDebugSkipCount = 0

set-alias bp Set-Breakpoint -Option AllScope -Description "PSCX function alias"
function Set-Breakpoint([bool]$displaySkipCount = $false)
{
    if (!$global:__pscxDebugBreakpointsEnabled) { return }
	
    if ($global:__pscxDebugSkipCount-- -gt 0) {
		if ($displaySkipCount) {
			Write-Host "Skipping breakpoint, skip count is $global:__pscxDebugSkipCount"
		}
		return 
	}
    
    $scriptName = $MyInvocation.ScriptName
    $lineNumber = $MyInvocation.ScriptLineNumber
    function prompt
    {
        "Bkpt scope:{0} line:{1} <Type 'exit' to continue execution>`n! " -f 
            $(if ([String]::IsNullOrEmpty($scriptName)) { 
                "globalscope", "<unknown>" 
              } 
              else { 
                (Split-Path $scriptName -Leaf), $lineNumber
              })
    }
    $host.EnterNestedPrompt()
}

set-alias ebp Enable-Breakpoints -Option AllScope -Description "PSCX function alias"
function Enable-Breakpoints {
	$global:__pscxDebugBreakpointsEnabled = $true
	Write-Host "Breakpoints enabled"
}

set-alias dbp Disable-Breakpoints -Option AllScope -Description "PSCX function alias"
function Disable-Breakpoints {
	$global:__pscxDebugBreakpointsEnabled = $false
	Write-Host "Breakpoints disabled"
}

set-alias skbp Skip-Breakpoints -Option AllScope -Description "PSCX function alias"
function Skip-Breakpoints([int]$num) {
	$global:__pscxDebugSkipCount = $num
}
